#include "fastnav.h"
#include <iostream>

FastNav::FastNav(QWidget *parent): QWidget(parent)
{
   showPreview = false;
   thumbHeight = 150;
   prevHeight = 150;
   // cal rows and columns
   rows = size().height() / (thumbHeight+1);
   columns = size().width() /(thumbWidth+1);
   currentPage = -1;
   lastPage = -1;
   setPalette(QPalette(QPalette::Window,(QColor(127,127,127))));
   setAutoFillBackground(true);

   setMouseTracking(true);

  
   // init test data
   last = QPointF(-1,-1);
 
   
}						

FastNav::~FastNav()
{
  
}
int FastNav::getPreviewTimes()
{
   return previewTimes;
}
double FastNav::getDistance()
{
   return  travelDistance;
}
void FastNav::cleanHistory()
{
   lastPage = -1;
}

// init Fast Nav object
void FastNav::initDoc(QVector<QImage> p)
{
  currentPage = -1;
  lastPage = -1;
  previews.clear();
  thumbs.clear();
  //set thumbs and previews
 
  
  if(p.size())
  {
     double ratio = (double)p[0].width() /(double)p[0].height();
     thumbWidth = (int)((double)thumbHeight * ratio);
     prevWidth = (int)((double)prevHeight * ratio);
  
  }

 
  for(int i = 0 ; i < p.size() ; ++i)
  {
     previews.push_back(p[i].scaled(prevWidth,prevHeight,Qt::IgnoreAspectRatio,Qt::SmoothTransformation));
     thumbs.push_back(p[i].scaled(thumbWidth,thumbHeight ,Qt::IgnoreAspectRatio,Qt::SmoothTransformation));
  }
  getGrid();
  // resized = false;
  update();
}

void FastNav::setTargetPage(int t)
{
   targetPage = t-1; // Note index in this widget is 0 based;
}

int  FastNav::getTargetClickTimes()
{
   return targetClickTimes;
}


//function to start collect data
void FastNav::beginTest()
{
   startTest = true;
   previewTimes = 0;
   travelDistance = 0.0;
   targetClickTimes = 0;
}

// function to abort testing
void FastNav::abortTest()
{
   startTest = false;
}


//Function handle double click event
void FastNav::mouseDoubleClickEvent (QMouseEvent * event )
{

   int x = event->x();
   int y = event->y();
   //!NOTE this is magic check this later
   mouseHover = columns * ( (y - 1) / (thumbHeight + 1)) + (x - 1) / (thumbWidth + 1);
   // if clicked page within range
   if( mouseHover > -1 &&  mouseHover < thumbs.size())
   {
      currentPage =  mouseHover;
      emit pageChanged(currentPage + 1);   
      lastPage = currentPage;

       //  showPreview = false;
      if(showPreview)
      {
	 showPreview = false;
	 if(startTest)
	 {
	    if(currentPage == targetPage)
	       targetClickTimes++;
	    previewTimes++;
	 }
      }
   }
}
//Function handle mouse move event
void FastNav::mouseMoveEvent (QMouseEvent * event)
{
   
   int x = event->x();
   int y = event->y();
   
   //!NOTE this is magic check this later
   mouseHover =  columns * ( (y - 1) / (thumbHeight + 1)) + (x - 1) / (thumbWidth + 1);
   currentPage = mouseHover;

   // track mouse move distance
   if(startTest)
   {
      if(last == QPointF(-1,-1))
	 last = event->posF();
      else
      {
	 QPointF current = event->posF();
	 QLineF line(last,current);
	 travelDistance += line.length();
	 last = current;
      }
   }
   
   update();
}

void FastNav::enterEvent(QEvent *)
{
   showPreview = true;
   update();
}

void FastNav::leaveEvent(QEvent*)
{

   showPreview = false;
   update();
}

// handle resize event
void FastNav::resizeEvent ( QResizeEvent *)
{ 
   getGrid();
}

void FastNav::paintEvent (QPaintEvent *)
{
   QPainter painter(this);
   painter.setRenderHint(QPainter::HighQualityAntialiasing);
  
    int x = 1 ; 
    int y = 1;
    int count = 0;
    

    // high lighting  boundy of last page
    if(lastPage > -1 && lastPage < thumbs.size ())
       painter.fillRect(lastPage % columns * (thumbWidth+1),lastPage/ columns * (thumbHeight+1),thumbWidth + 2,thumbHeight + 2,QBrush(QColor(255,0,0)));

   // highlighting boundy of  current page
   if( mouseHover > -1  &&  mouseHover < thumbs.size())
      painter.fillRect( mouseHover % columns * (thumbWidth+1), mouseHover/ columns * (thumbHeight+1),thumbWidth + 2,thumbHeight + 2,QBrush(QColor(0,255,0)));
   
   
   // paint thumbs
    for(int i = 0 ; i < thumbs.size() ; ++i)
    {
       painter.drawImage(QRectF(x,y,thumbWidth,thumbHeight),thumbs[i]);
       //painter.drawImage(QPoint(x,y),thumbs[i]);
       count ++;
       x += (thumbWidth + 1);
       x = x % (columns * (thumbWidth + 1));
       if(count == columns)
       {
          count = 0;
          y += (thumbHeight + 1);
       }
    }
 
    // high lighting  last page
    if(lastPage > -1 && lastPage < thumbs.size ())
       painter.fillRect(lastPage % columns * (thumbWidth+1),lastPage/ columns * (thumbHeight+1),thumbWidth + 2,thumbHeight + 2,QBrush(QColor(255,0,0,30)));


    // highlighting current page
   if( mouseHover > -1  &&  mouseHover < thumbs.size())
      painter.fillRect( mouseHover % columns * (thumbWidth+1), mouseHover/ columns * (thumbHeight+1),thumbWidth + 2,thumbHeight + 2,QBrush(QColor(0,255,0,30)));
   
 
    // paint preview 
   if(currentPage > -1  && currentPage < previews.size () && prevHeight > thumbHeight && showPreview)
   {
 
      // show previews
      QCursor cursor;
      QPoint p = mapFromGlobal(cursor.pos());
      int x = p.x();
      int y = p.y();
      //ensure always  shows full  preview 
      int wb = width();
      int hb = height();
      
      // check for 4 boundry 
      if(x <= wb  - prevWidth && y <= hb -prevHeight) 
      {
	 painter.fillRect(x-2,y-2,prevWidth+4,prevHeight+4,QBrush(QColor(0,0,0,127)));
	 painter.drawImage(p,previews[currentPage]);
      }
      else if(x >  wb - prevWidth && y <= hb-prevHeight )
      {
	 painter.fillRect(x-2-prevWidth,y-2,prevWidth+4,prevHeight+4,QBrush(QColor(0,0,0,127)));
	 painter.drawImage(QPoint(x-prevWidth,y),previews[currentPage]);
      }
      else if(x <=  wb  - prevWidth && y > hb -prevHeight)
      {
	 painter.fillRect(x-2,y-2-prevHeight,prevWidth+4,prevHeight+4,QBrush(QColor(0,0,0,127)));
	 painter.drawImage(QPoint(x,y-prevHeight),previews[currentPage]);
      }
      else if(x > wb - prevWidth && hb > height() -prevHeight)
      {
	 painter.fillRect(x-2-prevWidth,y-2-prevHeight,prevWidth+4,prevHeight+4,QBrush(QColor(0,0,0,127)));
	 painter.drawImage(QPoint(x-prevWidth,y-prevHeight),previews[currentPage]);
      }
      else
      {
	 painter.fillRect(x-2,y-2,prevWidth+4,prevHeight+4,QBrush(QColor(0,0,0,127)));
	 painter.drawImage(p,previews[currentPage]);
      }

      
   }
}

void FastNav::getGrid()
{
   
   thumbHeight = 151;

   double ratio = (double)thumbs[0].width() /(double)thumbs[0].height();
   // compute the grid system and height and width of thumb
   do
   {
      thumbHeight --;
      thumbWidth = (int)((double)thumbHeight * ratio);
      columns = width() /(thumbWidth+1);
      rows = height() /(thumbHeight+1); 
   }while(columns * rows  < thumbs.size());
   
   update();
   
}
